package cn.com.bsoft.mobile.service

import cn.com.bsoft.mobile.dao.MobileAppDao
import cn.com.bsoft.mobile.dao.MobileAppSuiteDao
import cn.com.bsoft.mobile.entity.MobileApp
import cn.com.bsoft.mobile.entity.MobileAppSuite
import cn.com.bsoft.mobile.model.App
import cn.com.bsoft.mobile.model.DeviceType
import org.springframework.beans.factory.annotation.Autowired

interface MobileAppService {
    fun setupApp(app: App): MobileApp
    fun saveApp(app: App): MobileApp
    fun queryApp(type: DeviceType, nativeID: String): MobileApp?
    fun queryApp(appID:String):MobileApp?
    fun allApp():List<MobileApp>
    fun allAppRsp():List<App>

    fun saveSuite(suiteID:String,title:String,vararg apps:MobileApp):MobileAppSuite
    fun setupSuite(suiteID:String,title:String,vararg apps:MobileApp):MobileAppSuite
    fun allSuite():List<MobileAppSuite>
    fun querySuite(suiteID: String):MobileAppSuite?

}

fun MobileApp.toRsp():App{
    val app = App(nativeID, DeviceType.fromString(type),title)

    app.teamID = teamID
    app.pushHost = pushHost
    app.pushKeyID = pushKeyID
    app.pushSecret = pushSecret

    app.suiteID = suite?.suiteID

    return app
}

class DefaultMobileAppService : MobileAppService {


    @Autowired
    lateinit var appDao: MobileAppDao

    @Autowired
    lateinit var suiteDao:MobileAppSuiteDao

    override fun allApp(): List<MobileApp> {
        return appDao.findAll()
    }

    override fun allAppRsp(): List<App> {
        return allApp().map {
            it.toRsp()
        }
    }

    override fun queryApp(appID: String): MobileApp? {
        return appDao.findOne(appID)
    }

    override fun setupApp(app: App): MobileApp {
        var mobileApp = appDao.findOne(app.appID)
        if (mobileApp == null) {
            mobileApp = saveApp(app)
        }
        return mobileApp
    }

    override fun saveApp(app: App): MobileApp {
        var mobileApp = MobileApp(app.appID,app.type.value)
        mobileApp.title = app.title

        if(app.teamID!=null)
            mobileApp.teamID = app.teamID

        if (app.pushHost != null)
            mobileApp.pushHost = app.pushHost

        if (app.pushKeyID != null)
            mobileApp.pushKeyID  = app.pushKeyID

        if (app.pushSecret != null)
            mobileApp.pushSecret = app.pushSecret

        if (app.suiteID!=null){
            val suite = querySuite(app.suiteID!!)
            mobileApp.suite = suite
        }

        return appDao.saveAndFlush(mobileApp)
    }

    override fun queryApp(type: DeviceType, nativeID: String): MobileApp? {
        return appDao.findByTypeAndNativeID(type.value,nativeID)
    }

    //--------------------------------

    override fun saveSuite(suiteID: String, title: String, vararg apps: MobileApp): MobileAppSuite {
        var suite = MobileAppSuite(suiteID,title)

        suite = suiteDao.saveAndFlush(suite)

        apps?.forEach {
            it.suite = suite
            appDao.saveAndFlush(it)
        }

        return suite
    }

    override fun setupSuite(suiteID: String, title: String, vararg apps: MobileApp): MobileAppSuite {
        var suite = querySuite(suiteID)?:saveSuite(suiteID,title,*apps)

        return suite
    }

    override fun allSuite(): List<MobileAppSuite> {
        return suiteDao.findAll()
    }

    override fun querySuite(suiteID: String): MobileAppSuite? {
        return suiteDao.findOne(suiteID)
    }

}